//这是计时器
var sp=document.querySelector('#timer-span');
//按钮 虚拟竞赛进行中 或者放弃


var timer;//计时器
function get(time) {
    var s=Math.floor((time/1000)%60);
    var m=Math.floor((time/1000/60)%60);
    var h=Math.floor((time/1000/3600)%24);
    var d=Math.floor(time/1000/3600/24);
    var jieguo=h+'小时'+m+'分钟'+s+'秒';
    return jieguo;
}
//判断之前是否有正在运行的虚拟竞赛
window.onload =function(){
    var ajax = new XMLHttpRequest() || new ActiveXObject('Microsoft.XMLHTTP');
    ajax.open("GET",ROOTSources+"/contest/determineVirtualContestStatus/"+contestId+"/"+customerId,true);
    ajax.send();
    ajax.onreadystatechange=function () {
        if (ajax.readyState === 4) {
            if (ajax.status >= 200 && ajax.status < 300 || ajax.status === 304) {
                console.log(111);
                var result=JSON.parse(ajax.responseText);
                console.log(result.data.contestMsgMark != null);

                if (result.data.contestMsgStartTime !=null && result.data.contestMsgThroughTime == null){
                    giveBtn.innerText="放弃虚拟竞赛";
                    giveBtn.className="btn btn-danger hover-panel";
                    daojishi();
                    console.log(1222);
                }else {
                    giveBtn.className="btn btn-primary";
                    giveBtn.innerText="开启虚拟竞赛";
                    sp.innerHTML="虚拟竞赛待开启";
                    for (var i = 0;i<usualHref.length;i++) {
                        usualHref[i].setAttribute("style","pointer-events: none;");
                    }
                }
            }
        }
    };
};


//相当于改变竞赛的状态
giveBtn.onclick=function () {
    if (giveBtn.innerText=="放弃虚拟竞赛"){
        clearInterval(timer);
        var ajaxEnd = new XMLHttpRequest() || new ActiveXObject('Microsoft.XMLHTTP');
        ajaxEnd.open("GET",ROOTSources+"/contest/endVirtualContest/"+contestId+"/"+customerId,true);
        ajaxEnd.send();
        ajaxEnd.onreadystatechange=function () {
            if (ajaxEnd.readyState === 4) {
                if (ajaxEnd.status >= 200 && ajaxEnd.status < 300 || ajaxEnd.status === 304) {
                    var result=JSON.parse(ajaxEnd.responseText);
                    alert("您已结束本场虚拟竞赛");
                    giveBtn.className="btn btn-primary";
                    giveBtn.innerText="开启虚拟竞赛";
                    sp.innerHTML="虚拟竞赛待开启";
                    localStorage.removeItem("time");
                }
            }
        };
    }
    else if (giveBtn.innerText=="开启虚拟竞赛"){
        var ajax = new XMLHttpRequest() || new ActiveXObject('Microsoft.XMLHTTP');
        ajax.open("GET",ROOTSources+"/contest/beginVirtualContest/"+contestId+"/"+customerId,true);
        ajax.send();
        ajax.onreadystatechange =function () {
            if (ajax.readyState ===4) {
                if (ajax.status >= 200 && ajax.status < 300 || ajax.status === 304) {
                    clearInterval(timer);//清空倒计时
                    var time=new Date().getTime();//存储当前时间
                    alert("虚拟竞赛已开启");
                    localStorage.setItem("time",time);
                    giveBtn.innerText="放弃虚拟竞赛";
                    giveBtn.className="btn btn-danger hover-panel";
                    daojishi();
                    window.history.go(0);
                }
            }
        }
    }
};

function daojishi() {
    var time=parseInt(localStorage.getItem("time"));
    var end_time1=time+5400000;
    console.log(end_time1);

    var end_time=new Date(end_time1);
    console.log(end_time);

    timer = setInterval(function timeFun() {
        var new_time=new Date();
        // if (end_time===new_time) {
        //     // 完成
        // }
        var rtime=end_time-new_time;
        console.log("===================="+rtime);
        var f=get(rtime);
        console.log(f);
        sp.innerHTML=f;
    },500);
}